using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using PointOfSale.Global;
using System.Data .SqlClient;
using PointOfSale.Class;
namespace PointOfSale.Configuration.Department
{
	public class frmDepartmentMaster : System.Windows.Forms.Form
	{
		//
		//Components of the form
		//
		private Joaqs.UI.XpGroupBox xpGroupBox1;
		private Newco.Controls.LabelLine labelLine1;
		private Joaqs.UI.InputTextBox txtDepartmentName;
		public static XPExplorerBar.XPListView listView;
		private Joaqs.UI.XpButton btnAdd;
		private Joaqs.UI.XpButton btnUpdate;
		private Joaqs.UI.XpButton btnDelete;
		private Joaqs.UI.XpButton btnOK;
		private System.Windows.Forms.ColumnHeader columnHeader2;
		private System.Windows.Forms.ColumnHeader columnHeader3;

		//
		//variable for listView
		//
		private ListView.SelectedListViewItemCollection row = null;
		private ListView.CheckedListViewItemCollection checkedRow = null;
		private string strDepartmentID = null;
		private string strCurrentDepartmentName = null;
		private int intRowIndex = 0;

		private System.ComponentModel.Container components = null;

		public frmDepartmentMaster()
		{
			InitializeComponent();
		}

		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.xpGroupBox1 = new Joaqs.UI.XpGroupBox();
			this.btnDelete = new Joaqs.UI.XpButton();
			this.btnUpdate = new Joaqs.UI.XpButton();
			this.btnAdd = new Joaqs.UI.XpButton();
			listView = new XPExplorerBar.XPListView();
			this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader3 = new System.Windows.Forms.ColumnHeader();
			this.txtDepartmentName = new Joaqs.UI.InputTextBox();
			this.labelLine1 = new Newco.Controls.LabelLine();
			this.btnOK = new Joaqs.UI.XpButton();
			this.xpGroupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// xpGroupBox1
			// 
			this.xpGroupBox1.Controls.Add(this.btnDelete);
			this.xpGroupBox1.Controls.Add(this.btnUpdate);
			this.xpGroupBox1.Controls.Add(this.btnAdd);
			this.xpGroupBox1.Controls.Add(listView);
			this.xpGroupBox1.Controls.Add(this.txtDepartmentName);
			this.xpGroupBox1.Controls.Add(this.labelLine1);
			this.xpGroupBox1.Location = new System.Drawing.Point(8, 8);
			this.xpGroupBox1.Name = "xpGroupBox1";
			this.xpGroupBox1.Size = new System.Drawing.Size(280, 272);
			this.xpGroupBox1.TabIndex = 0;
			this.xpGroupBox1.TabStop = false;
			this.xpGroupBox1.Text = "Department Based Operations";
			// 
			// btnDelete
			// 
			this.btnDelete.AdjustImageLocation = new System.Drawing.Point(0, 0);
			this.btnDelete.BtnShape = Joaqs.UI.emunType.BtnShape.Rectangle;
			this.btnDelete.BtnStyle = Joaqs.UI.emunType.XPStyle.Default;
			this.btnDelete.Location = new System.Drawing.Point(200, 240);
			this.btnDelete.Name = "btnDelete";
			this.btnDelete.TabIndex = 5;
			this.btnDelete.Text = "Delete";
			this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
			// 
			// btnUpdate
			// 
			this.btnUpdate.AdjustImageLocation = new System.Drawing.Point(0, 0);
			this.btnUpdate.BtnShape = Joaqs.UI.emunType.BtnShape.Rectangle;
			this.btnUpdate.BtnStyle = Joaqs.UI.emunType.XPStyle.Default;
			this.btnUpdate.Location = new System.Drawing.Point(112, 240);
			this.btnUpdate.Name = "btnUpdate";
			this.btnUpdate.TabIndex = 4;
			this.btnUpdate.Text = "Update";
			this.btnUpdate.Click += new System.EventHandler(this.btnUpdate_Click);
			// 
			// btnAdd
			// 
			this.btnAdd.AdjustImageLocation = new System.Drawing.Point(0, 0);
			this.btnAdd.BtnShape = Joaqs.UI.emunType.BtnShape.Rectangle;
			this.btnAdd.BtnStyle = Joaqs.UI.emunType.XPStyle.Default;
			this.btnAdd.Location = new System.Drawing.Point(24, 240);
			this.btnAdd.Name = "btnAdd";
			this.btnAdd.TabIndex = 3;
			this.btnAdd.Text = "Add";
			this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
			// 
			// listView
			// 
			listView.CheckBoxes = true;
			listView.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																					   this.columnHeader2,
																					   this.columnHeader3});
			listView.FullRowSelect = true;
			listView.GridLines = true;
			listView.ImeMode = System.Windows.Forms.ImeMode.Off;
			listView.Location = new System.Drawing.Point(8, 56);
			listView.MultiSelect = false;
			listView.Name = "listView";
			listView.Size = new System.Drawing.Size(264, 176);
			listView.TabIndex = 2;
			listView.View = System.Windows.Forms.View.Details;
			listView.Click += new System.EventHandler(this.listView_Click);
			listView.MouseUp += new System.Windows.Forms.MouseEventHandler(this.listView_MouseUp);
			listView.ItemCheck += new System.Windows.Forms.ItemCheckEventHandler(this.listView_ItemCheck);
			// 
			// columnHeader2
			// 
			this.columnHeader2.Text = "Department Name";
			this.columnHeader2.Width = 260;
			// 
			// columnHeader3
			// 
			this.columnHeader3.Text = "DepartmentID";
			this.columnHeader3.Width = 0;
			// 
			// txtDepartmentName
			// 
			this.txtDepartmentName.Location = new System.Drawing.Point(88, 24);
			this.txtDepartmentName.Name = "txtDepartmentName";
			this.txtDepartmentName.Size = new System.Drawing.Size(184, 20);
			this.txtDepartmentName.TabIndex = 1;
			this.txtDepartmentName.Text = "";
			// 
			// labelLine1
			// 
			this.labelLine1.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(0)), ((System.Byte)(70)), ((System.Byte)(213)));
			this.labelLine1.LineColor = System.Drawing.Color.FromArgb(((System.Byte)(208)), ((System.Byte)(208)), ((System.Byte)(191)));
			this.labelLine1.Location = new System.Drawing.Point(8, 24);
			this.labelLine1.Name = "labelLine1";
			this.labelLine1.Size = new System.Drawing.Size(80, 23);
			this.labelLine1.TabIndex = 0;
			this.labelLine1.Text = "Department";
			// 
			// btnOK
			// 
			this.btnOK.AdjustImageLocation = new System.Drawing.Point(0, 0);
			this.btnOK.BtnShape = Joaqs.UI.emunType.BtnShape.Rectangle;
			this.btnOK.BtnStyle = Joaqs.UI.emunType.XPStyle.Default;
			this.btnOK.Location = new System.Drawing.Point(208, 288);
			this.btnOK.Name = "btnOK";
			this.btnOK.Size = new System.Drawing.Size(75, 32);
			this.btnOK.TabIndex = 6;
			this.btnOK.Text = "OK";
			this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
			// 
			// frmDepartmentMaster
			// 
			this.AcceptButton = this.btnOK;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(296, 326);
			this.Controls.Add(this.xpGroupBox1);
			this.Controls.Add(this.btnOK);
			this.Name = "frmDepartmentMaster";
			this.Text = "Department master";
			this.Load += new System.EventHandler(this.frmDepartmentMaster_Load);
			this.Activated += new System.EventHandler(this.frmDepartmentMaster_Activated);
			this.xpGroupBox1.ResumeLayout(false);
			this.ResumeLayout(false);

			this.xpGroupBox1.Text = clsGlobal.g_Hashtable["DepartmentOperation"].ToString();
			this.btnDelete.Text = clsGlobal.g_Hashtable["DepartmentbtnDelete"].ToString();
			this.btnUpdate.Text = clsGlobal.g_Hashtable["DepartmentbtnUpdate"].ToString();
			this.btnAdd.Text = clsGlobal.g_Hashtable["DepartmentbtnAdd"].ToString();
			this.columnHeader2.Text = clsGlobal.g_Hashtable["DeparmentName"].ToString();
			this.labelLine1.Text = clsGlobal.g_Hashtable["Department"].ToString();
			this.btnOK.Text = clsGlobal.g_Hashtable["DepartmentbtnOK"].ToString();
			this.Text = clsGlobal.g_Hashtable["DepartmentFormName"].ToString();
		}
		#endregion

		//
		//Load data from table tblDepartment to listView
		//
		public void LoadDataToListView()
		{
			SqlDataReader dr = clsDepartment.GetAllDataToDataReader();
			
			string strDepartmentID;
			string strDepartmentName;
			listView.Items.Clear();

			while(dr.Read ())
			{
				strDepartmentID = dr["DepartmentID"].ToString();
				strDepartmentName = dr["DepartmentName"].ToString();
				ListViewItem TempListViewItem = new ListViewItem (new string[]{ strDepartmentName, strDepartmentID});
				listView.Items .Add (TempListViewItem);
			}
			dr.Close ();
		}

		private void frmDepartmentMaster_Load(object sender, System.EventArgs e)
		{
			LoadDataToListView();
			this.btnUpdate.Enabled = false;
			this.btnDelete.Enabled = false;
			this.txtDepartmentName.Enabled = false;
		}

		private void btnOK_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void listView_Click(object sender, System.EventArgs e)
		{
			this.row = listView.SelectedItems;
			if(this.row.Count != 0)
			{
				this.strDepartmentID = listView.FocusedItem.SubItems[1].Text;

				this.strCurrentDepartmentName = listView.FocusedItem.SubItems[0].Text;

				this.intRowIndex = listView.FocusedItem.Index;

				this.txtDepartmentName.Enabled = true;
				this.txtDepartmentName.Text = listView.FocusedItem.SubItems[0].Text;

				this.btnUpdate.Enabled = true;
				this.AcceptButton = this.btnUpdate;
			}
		}

		
		private void btnUpdate_Click(object sender, System.EventArgs e)
		{
			string strDepartmentName = this.txtDepartmentName.Text;
			bool isNewItem = true;
			SqlDataReader dr = clsDepartment.GetAllDataToDataReader();
			while(dr.Read())
			{
				string strTempDepartmentName = dr["DepartmentName"].ToString();
				if(String.Compare(strDepartmentName, strTempDepartmentName, true) == 0)
				{
					isNewItem = false;
				}
			}
			dr.Close();
			if(String.Compare(strCurrentDepartmentName, strDepartmentName, true) == 0)
			{
				isNewItem = true;
			}

			if(strDepartmentName.Length == 0)
			{
				MessageBox.Show("Department Name must not null!");
				this.ActiveControl = this.txtDepartmentName;
			}
			else
			{
				if(!isNewItem)
				{
					MessageBox.Show("This Department name is already exist in database!");
					this.ActiveControl = this.txtDepartmentName;
				}
				else
				{
					clsDepartment dept = new clsDepartment();
					dept.Id = Convert.ToInt32(strDepartmentID);
					dept.Update(strDepartmentName);

					listView.Items[intRowIndex].SubItems[0].Text = strDepartmentName;	
				
					this.btnUpdate.Enabled = false;

					this.txtDepartmentName.Enabled = false;
					this.txtDepartmentName.Text = "";

					this.AcceptButton = this.btnOK;
				}
			}
		}

		private void btnDelete_Click(object sender, System.EventArgs e)
		{
			this.checkedRow = listView.CheckedItems;
			string strDepartmentID = null;
			foreach(ListViewItem item in this.checkedRow)
			{
				strDepartmentID = item.SubItems[1].Text;
				clsDepartment.Delete(Convert.ToInt32(strDepartmentID));
				
				if(item.Selected)
				{
					this.btnUpdate.Enabled = false;
				}
				
				listView.Items.Remove(item);
			}
			this.btnDelete.Enabled = false;
			this.AcceptButton = this.btnOK;
		}

		private void btnAdd_Click(object sender, System.EventArgs e)
		{
			frmAddDepartment frmAdd = new frmAddDepartment();
			frmAdd.Show();
		}

		private void frmDepartmentMaster_Activated(object sender, System.EventArgs e)
		{
			LoadDataToListView();
			this.AcceptButton = this.btnOK;
		}

		private void listView_ItemCheck(object sender, System.Windows.Forms.ItemCheckEventArgs e)
		{
			this.AcceptButton = this.btnDelete;
		}

		private void listView_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
		{	
			this.row = listView.SelectedItems;
			this.checkedRow = listView.CheckedItems;
			if(this.checkedRow.Count == 0)
			{
				this.btnDelete.Enabled = false;

				if(this.row.Count == 0)
				{
					this.AcceptButton = this.btnOK;
				}
			}
			else
			{
				this.btnDelete.Enabled = true;
			}

			if(this.row.Count == 0)
			{
				this.btnUpdate.Enabled = false;

				this.txtDepartmentName.Enabled = false;
				this.txtDepartmentName.Text = "";

				if(this.checkedRow.Count == 0)
				{
					this.AcceptButton = this.btnOK;
				}
			}
			else
			{
				this.btnUpdate.Enabled = true;
				this.txtDepartmentName.Enabled = true;
			}
		}

		public delegate void Call( string s );

		public static void CallMethod( string s )
		{
			ListViewItem tempItem = new ListViewItem(new string[]{s, ""});
			listView.Items.Add(tempItem);
		}
	}
}
